<?php

namespace App\Http\Controllers;

use App\Models\Question;
use Illuminate\Http\Request;

class QuestionCommentsController extends Controller
{
    //

    /**
     * QuestionCommentsController constructor.
     */
    public function __construct()
    {
        $this->middleware('auth')->except(['index']);
    }

    public function store(Question $question)
    {
        $this->validate(\request(), [
            'content' => 'required'
        ]);
        $question = Question::published()->findOrFail($question->id);


        $comment =  $question->comment(request('content'),auth()->id());
        return $comment->load('owner');
    }

    public function index(Question $question)
    {
        $comments =  $question->comments()->paginate(10);
        array_map(function (&$item) {
            return $this->appendVotedAttribute($item);
        },$comments->items());
        return $comments;
    }
}
